package com.mofang.powerdekorhelper.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.mofang.powerdekorhelper.utils.ConnectionUtils;
import com.mofang.powerdekorhelper.utils.Countdown;
/**
 * Created by luofei on 2017/12/22 0022.
 * 说明：
 */

public class HeartbeatService extends Service {

    private Context mContexct;
    private Countdown timer;
    private ConnectionUtils connectionUtils;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void startService(Context context) {
        this.mContexct = context;

        //connectionUtils.init(context, UrlTools.WSUrl);
        Intent intent = new Intent(context, HeartbeatService.class);
        mContexct.startService(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        timer = new Countdown();
        timer.setDivideTime(10 * 1000);
        timer.setStop(false);
        timer.createTimer(new Countdown.UpDataTime() {
            @Override
            public void updataTime(int time) {
                connectionUtils = ConnectionUtils.getInstance();
                connectionUtils.send("hello");
            }
        });

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean stopService(Intent name) {
        if (timer != null) {
            timer.stop();
        }
        return super.stopService(name);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.stop();
        }
    }
}
